# 静态方法
class Person(object):
    @staticmethod
    def study(name):
        print(f"{name}学习中...")


Person.study("zhangsan")
pe = Person()
pe.study("lisi")


# 类方法
class Person(object):
    name = "zhansan"

    @classmethod
    def sleep(cls):
        print("睡觉", cls)
        print(cls.name)


Person.sleep()


class Person(object):
    name = "zhangsan"  # 类属性

    def __init__(self):
        self.age = 18  # 实例属性

    # 实例方法
    def play(self):
        print(f"{Person.name}")
        print(f"{self.age}")

    # 静态方法
    @staticmethod
    def introduce():
        print(f"静态方法 {Person.name}")

    # 类方法
    @classmethod
    def introduce2(cls):
        print(cls.name)


pe = Person()
pe.play()
pe.introduce()
pe.introduce2()


# 单例模式
# class A(object):
#     pass
#
#
# a1 = A()
# print(a1)
# a2 = A()
# print(a2)
# 重写 __new__() 实现单例
class Sington(object):
    obj = None

    def __new__(cls, *args, **kwargs):
        print("重写了__new__")
        if cls.obj is None:
            cls.obj = super().__new__(cls)
        return cls.obj

    def __init__(self):
        print("重写了__init__")


s = Sington()
print(s)
s2 = Sington()
print(s2)
# 导入模块实现单例
from m import te as te1
from m import te as te2

print(id(te1))
print(id(te2))


# 魔法方法
# __doc__ 类的描述信息
class Person(object):
    """描述信息"""
    pass


print(Person.__doc__)


def sing():
    """唱歌"""


print(sing.__doc__)
# __module__ 输出模块名
# __class__ 输出所在类信息
import m

b = m.B()
print(b)
b.funa()
print(b.__module__)
print(b.__class__)


#  __str__ 对象描述信息

class C:
    def __str__(self):
        return "这里是__str__返回值"  # 这里必须返回字符串类型


c = C()
print(c)


# __del__() 析构函数
# __call__() 使一个对象成为可调用对象
# callable() 判断一个对象是否可调用对象
def func():
    print("呵呵")


func()
print(callable(func))


class A:
    def __call__(self, *args, **kwargs):
        print("这是__call__")


a = A()
a()
print(callable(a))
